package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/29.
 */
public class UserController {
    UserService userService = new UserServiceImpl();
    @URLMapping(url = "/adminUser")
    public String userList(HttpServletRequest request, HttpServletResponse response){
        List<User> list=userService.findUserList();
        request.setAttribute("list",list);
        return "/admin/user/list.jsp";
    }
    //删除
    @URLMapping(url = "/deleteUser")
    public String deleteUser(HttpServletRequest request,HttpServletResponse response){
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            int count=userService.deleteUser(user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/adminUser";
    }
    @URLMapping(url = "/loginUI")
    public String loginUI(HttpServletRequest request, HttpServletResponse response){

        return "/WEB-INF/user/login.jsp";
    }
    //跳转到注册页面
    @URLMapping(url = "/registUI")
    public String registUI(HttpServletRequest request, HttpServletResponse response){

        return "/WEB-INF/user/regist.jsp";
    }
    @URLMapping(url = "/regist")
    public String regist(HttpServletRequest request, HttpServletResponse response){
        String inputCode = request.getParameter("code");
        String trueCode = (String) request.getSession().getAttribute("validatecCode");
        if (!inputCode.equalsIgnoreCase(trueCode)){
            request.setAttribute("codeMsg","验证码错误");
            return "/WEB-INF/user/regist.jsp";
        }
        User user = new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            int count = userService.save(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //登录激活提示登录页面
        request.setAttribute("msg","注册成功，请先激活在使用");
        return "/WEB-INF/user/msg.jsp";
    }

    @URLMapping(url = "/findByUsername")
    public String findByUsername(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");

        User user =  userService.findByUsername(username);
        if (user==null){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
        return null;
    }

    @URLMapping(url = "/activeCode")
    public String activeCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");

        User user = userService.findByCode(code);
        if(user!=null){
            user.setCode(null);
            user.setState(1);
            int count = userService.update(user);
            request.setAttribute("msg","激活成功，请登录");
        }else {
            request.setAttribute("msg","激活失败，请先注册");
        }
        //response.getWriter().print("激活成功");
        //查询用户
        //User user = userService.findCode(code);
        //修改用户状态
        // user.setState(1);
        //更新用户
        //userService.update(user);
        return "/WEB-INF/user/msg.jsp";
    }

    @URLMapping(url = "/login")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.findByUsername(username);
        if (user==null){
            return "/WEB-INF/user/login.jsp";
        }else {
            request.getSession().setAttribute("user",user);
            return "Redirect:"+request.getContextPath()+"/index";
        }
    }
}
